package com.gmrz.service.logpool;

import com.gmrz.uaf.common.GuiceUtil;
import com.gmrz.uaf.db.DAOFactory;
import com.gmrz.uaf.db.UAFDAOFactory;
import com.gmrz.uaf.db.UAFDBConnectionMgr;
import com.gmrz.uaf.db.dao.FidoLogDAO;
import com.gmrz.uaf.protocol.v1.schema.FidoLog;
import com.gmrz.util.db.DBUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by chentaikun on 16/12/28.
 */
public class LogWorker implements Runnable {

    private static final Logger LOG = LogManager.getLogger(LogWorker.class);
    private List<FidoLog> list;

    public LogWorker(List<FidoLog> list){
        this.list = list;
    }
    public void run() {
        batchSaveLog();
        list = null;
    }

    private void batchSaveLog() {
        Connection conn = null;
        try {
            //FIDOLOG
            //conn = UAFDBConnectionMgr.getConnection(false);
            conn = UAFDBConnectionMgr.getLogConnection(false);
//            FidoLogDAO dao = GuiceUtil.getProcessorInjector().getInstance(DAOFactory.class).createFidoLogDAO(conn);
            FidoLogDAO dao = UAFDAOFactory.createFidoLogDAO(conn);
            dao.insert(list);
            conn.commit();
        }catch (SQLException sqle){
            LOG.error("DataBase connection error while saving FIDO Log",sqle);
        }catch (Exception e){
            LOG.error("Unknown error while saving FIDO Log",e);
        }finally {
            DBUtil.close(conn);
        }
    }
}
